package com.xr.web.controller.miniprogram;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xr.common.annotation.Log;
import com.xr.common.constant.OrderStatusConstants;
import com.xr.common.constant.StaffConstants;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.common.enums.BusinessType;
import com.xr.miniprogram.domain.MiniProgramOrder;
import com.xr.miniprogram.domain.MiniProgramOrderUser;
import com.xr.miniprogram.domain.MiniProgramStaff;
import com.xr.miniprogram.domain.vo.MiniProgramOrderVO;
import com.xr.miniprogram.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * description: 订单模块接口
 *
 * @author: wls
 * @date: 2023/11/9 9:37
 * @version:1.0
 */
@Api("订单模块接口")
@RestController
@RequestMapping("/mini")
public class MpOrderController extends BaseController {
    @Autowired
    private IMiniProgramOrderService miniProgramOrderService;
    @Autowired
    private IMiniProgramOrderStatusService miniProgramOrderStatusService;
    @Resource
    private IMiniProgramOrderUserService orderUserService;
    @Resource
    private IMiniProgramServiceLogService serviceLogService;
    @Resource
    private IMiniProgramStaffService staffService;
    @Resource
    private IMiniProgramRefundService refundService;
    @Resource
    private WXPayService wxPayService;

    /**
     * 分页查询订单树形列表
     * @param
     * @return
     */
    @ApiOperation("分页查询订单树形列表")
    @PreAuthorize("@ss.hasPermi('mini:order:list')")
    @RequestMapping("/order/select")
    public AjaxResult selects(MiniProgramOrderVO miniProgramOrderVO, Long statusCode, Long uStatusId, Long pageNum, Long pageSize, String staffName)
    {
        IPage<MiniProgramOrderVO> orderList = miniProgramOrderService.selectMiniProgramOrderList(new Page<>(pageNum,pageSize), miniProgramOrderVO.toMiniProgramOrder(), statusCode, uStatusId, staffName);
        return success(orderList);
    }

    /**
     * 获取订单标签统计
     */
    @ApiOperation("获取订单状态标签菜单")
    @PreAuthorize("@ss.hasPermi('mini:orderstatus:list')")
    @GetMapping(value = "/orderStatus/typeList")
    public AjaxResult getTypeCount()
    {
        return success(miniProgramOrderStatusService.getTypeList());
    }

    /**
     * 获取订单状态统计
     */
    @ApiOperation("获取订单状态标签菜单")
    @PreAuthorize("@ss.hasPermi('mini:orderstatus:list')")
    @GetMapping(value = "/orderStatus/statusTitleList")
    public AjaxResult getStatusTitleList()
    {
        return success(miniProgramOrderStatusService.getStatusTitleList());
    }

    /**
     * 根据orderNumber查询退款表
     * @return
     */
    @ApiOperation("根据orderNumber查询退款信息")
    @PreAuthorize("@ss.hasPermi('mini:refund:query')")
    @GetMapping("/order/getRefundInfo")
    public AjaxResult refund(String orderNumber)
    {
        //查询退款单
        return success(refundService.getRefundByOrderNumber(orderNumber));
    }

    @ApiOperation("批准退款")
    @Operation(summary = "交易订单退款申请" , description = "交易订单退款申请" )
    @PreAuthorize("@ss.hasPermi('mini:order:refund')")
    @RequestMapping("/pay/refundOrder/{orderNo}" )
    public AjaxResult reviewRefundById(@PathVariable("orderNo") String orderNo) throws Exception {
        wxPayService.refundOrder(orderNo);
        return success();
    }

    @ApiOperation("修改orderUser")
    @PreAuthorize("@ss.hasPermi('mini:orderuser:edit')")
    @PostMapping("/order/updateOrderUser")
    public AjaxResult updateOrderUser(@RequestBody MiniProgramOrderUser miniProgramOrderUser)
    {
        return success(orderUserService.updateOrderUser(miniProgramOrderUser));
    }

    /**
     * 为orderUser分配工人
     * @param serviceLogId
     * @param staffId
     * @return
     */
    @ApiOperation("为orderUser分配工人")
    @Transactional
    @PreAuthorize("@ss.hasPermi('mini:orderuser:edit')")
    @PostMapping("/order/selectStaff/{serviceLogId}/{staffId}")
    public AjaxResult selectStaff(@PathVariable("serviceLogId") Long serviceLogId, @PathVariable("staffId") Long staffId)
    {
        MiniProgramStaff newSelect = new MiniProgramStaff();
        newSelect.setStaffId(staffId);
        newSelect.setStaffStatus(StaffConstants.ASSIGNED);
        MiniProgramOrderUser miniProgramOrderUser = orderUserService.getByServiceLogId(serviceLogId);
        miniProgramOrderUser.setOrderStatusId(OrderStatusConstants.ASSIGNED_ORDER);

        return success(staffService.updateStaffOrder(serviceLogId, staffId) && staffService.updateById(newSelect) && orderUserService.updateById(miniProgramOrderUser));
    }

    /**
     * 导出order列表
     */
    @PreAuthorize("@ss.hasPermi('miniprogram:order:export')")
    @Log(title = "order", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniProgramOrder miniProgramOrder)
    {
//        List<MiniProgramOrder> list = miniProgramOrderService.selectMiniProgramOrderList(miniProgramOrder);
//        ExcelUtil<MiniProgramOrder> util = new ExcelUtil<MiniProgramOrder>(MiniProgramOrder.class);
//        util.exportExcel(response, list, "order数据");
    }
}
